using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

namespace Janus.NorthwindDemo2007
{
	public partial class ProductDataForm
	{

		protected override void StartEdition()
		{
			this.FillSuppliersAndCategories();
			this.ProductDataControl1.DataSource = this.DataSet;
			if (this.EditMode == DataFormEditMode.AddNew)
			{
				this.Text = "Products - New Product";
				this.ProductDataControl1.IsAddNew = true;
			}
			else
			{
				this.Text = "Product - " + ((DataRowView)CurrencyManager.Current)["ProductName"];
				this.ProductDataControl1.IsAddNew = false;
			}
			this.ProductDataControl1.Select();
		}


		private bool mOrdersFilled;
		private bool mRelatedTablesFilled;
		private void FillEmployeesValueList()
		{
			Janus.Windows.GridEX.GridEXColumn cEmp = null;
			cEmp = this.jsgProducts.RootTable.Columns["EmployeeID"];
			cEmp.ColumnType = Windows.GridEX.ColumnType.ImageAndText;
			cEmp.HasValueList = true;
			cEmp.EditType = Windows.GridEX.EditType.DropDownList;
			DataTable employees = NorthwindApp.NorthwindApplication.GetEmployeeList();
			cEmp.ValueList.PopulateValueList(new DataView(employees), "EmployeeID", "Name", "Photo", Color.Magenta, new Size(16, 16));

		}
		private void FillCustomersValueList()
		{
			Janus.Windows.GridEX.GridEXColumn cCust = null;
			cCust = this.jsgProducts.RootTable.Columns["CustomerID"];
			cCust.HasValueList = true;
			cCust.EditType = Windows.GridEX.EditType.DropDownList;
			DataTable customers = NorthwindApp.NorthwindApplication.GetCustomersList();
			cCust.ValueList.PopulateValueList(new DataView(customers), "CustomerID", "CompanyName");

		}
		private void FillOrders()
		{
			if (! mOrdersFilled)
			{
				if (! this.DataRowView.IsNew)
				{
					FillEmployeesValueList();
					FillCustomersValueList();
					NorthwindDataTableAdapters.OrderDetailsExtendedTableAdapter orderDetailsAdapter = new NorthwindDataTableAdapters.OrderDetailsExtendedTableAdapter();
					orderDetailsAdapter.FillByProduct((int)this.DataRowView["ProductID"], this.DataSet.OrderDetailsExtended);
				}
				this.jsgProducts.SetDataBinding(this.DataSet, "OrderDetailsExtended");
				mOrdersFilled = true;
			}

		}
		private void FillSuppliersAndCategories()
		{
			if (! mRelatedTablesFilled)
			{
				if (! this.DataRowView.IsNew)
				{
					this.Product_CategoriesTableAdapter1.FillByProduct((int)this.DataRowView["ProductID"], this.DataSet.Product_Categories);
					this.Suppliers_ProductsTableAdapter1.FillByProduct((int)this.DataRowView["ProductID"], this.DataSet.Suppliers_Products);

				}
			}

		}



		private void tabOrders_SelectedTabChanged(object sender, Janus.Windows.UI.Tab.TabEventArgs e)
		{
			if (e.Page == this.tabOrders)
			{
				this.FillOrders();
			}
		}
	}

} //end of root namespace